﻿function StaffTimeEditConfirmController($scope, service, $routeParams, $location, $http, $filter) {
    
    var popupOptions = {
        appendTo: $("#popup-wrapper"),
        closeClass: 'b-close',
        speed: 300
    };

    $scope.path = "/StaffTime";
    //Time
    var s = "2012-10-16T17:57:28.556094Z";
    $scope.v = {
        Dt: Date.now(),
        sDt: s,
        DDt: Date.parse(s)
    }

    $scope.staffStoreId = "";
    //get staff time table by Staff time table Id
    $http.get(service.locationUrl + "/stafftimeedit/" + $routeParams.Id + "/date/" + $routeParams.dateForgot.toString('yyyy-MMM-dd'), {})
        .success(function (data) {
            data.StampDate = $filter('date')(data.StampDate, 'yyyy-MM-dd');
            $scope.StaffList = data;
            console.log("abc", data);
        })
        .error(function (err) {

            console.log("error", err);
        });

    $http.get(service.locationUrl + "/storelistbystoreid/" + $routeParams.storeID, {})
       .success(function (data) {
           $scope.storeList = data;
       })
       .error(function (err) {
           console.log("error", err);
       });
    /// get data form params
    $scope.checkTypeUP = $routeParams.checkType;
    $scope.recordDateUP = $routeParams.recordDate;
    $scope.editTimeUP = $routeParams.editTime;
    $scope.remarkUP = $routeParams.remark;

    // back to staff edit page
    $scope.back = function ()
    {
        $location.path("/StaffTimeEdit/" + $routeParams.Id);
    }

    //get staff time table data for update
    $http.get(service.locationUrl + "/companystafftimetable/" + $routeParams.StaffTimeTableId, {})
      .success(function (data) {
          data.StampDate = $filter('date')(data.StampDate, 'yyyy-MM-dd');
          $scope.StaffObj = data;
      })
      .error(function (err) {
          console.log("error", err);
      });

    /// update the input to database
    $scope.save = function ()
    {
        $scope.title = "Do you want Save ?";
        $('.popup-delete-save').bPopup(popupOptions);

        $scope.action = function () {
            console.log("xyz");
            //time Update
            var dateUpdate = moment().format('L');

            // update staff time table on check in case
            if ($routeParams.checkType == 'Check In') {
                // obj staff time table use update on check in
                var objStaffTime =
                    {
                        CompanyStaffId: $scope.StaffObj.CompanyStaffId,
                        StampDate: $scope.StaffObj.StampDate,
                        UpdateTime: dateUpdate,
                        Remark: $routeParams.remark,
                        StoreId: $routeParams.storeID,
                        CheckInTime: $routeParams.recordDate,
                        CheckOutTime: $scope.StaffObj.CheckOutTime,
                        GoOutTime: $scope.StaffObj.GoOutTime,
                        TurnBackTime: $scope.StaffObj.TurnBackTime,
                        StartBreakTime: $scope.StaffObj.StartBreakTime,
                        EndBreakTime: $scope.StaffObj.EndBreakTime
                    }

                $http.put(
                    service.locationUrl + "/CompanyStaffTimeTable/" + $routeParams.StaffTimeTableId,
                    objStaffTime)
                .success(function () {
                    $location.path($scope.path);
                })
                .error(function () { });
            }
            //End update staff time table on check in case

            // update staff time table on check out case
            if ($routeParams.checkType == 'Check Out') {
                var objStaffTime =
                   {
                       CompanyStaffId: $scope.StaffObj.CompanyStaffId,
                       StampDate: $scope.StaffObj.StampDate,
                       StoreId: $routeParams.storeID,
                       UpdateTime: dateUpdate,
                       Remark: $routeParams.remark,
                       CheckInTime: $scope.StaffObj.CheckInTime,
                       CheckOutTime: $routeParams.recordDate,
                       GoOutTime: $scope.StaffObj.GoOutTime,
                       TurnBackTime: $scope.StaffObj.TurnBackTime,
                       StartBreakTime: $scope.StaffObj.StartBreakTime,
                       EndBreakTime: $scope.StaffObj.EndBreakTime
                   }

                $http.put(
                    service.locationUrl + "/CompanyStaffTimeTable/" + $routeParams.StaffTimeTableId,
                    objStaffTime)
                .success(function () {
                    $location.path($scope.path);
                })
                .error(function () { });
            }
            //update staff time table on Start Break case
            if ($routeParams.checkType == 'Start Break') {
                var objStaffTime =
                   {
                       CompanyStaffId: $scope.StaffObj.CompanyStaffId,
                       StampDate: $scope.StaffObj.StampDate,
                       StoreId: $routeParams.storeID,
                       UpdateTime: dateUpdate,
                       Remark: $routeParams.remark,
                       CheckInTime: $scope.StaffObj.CheckInTime,
                       CheckOutTime: $scope.StaffObj.CheckOutTime,
                       GoOutTime: $scope.StaffObj.GoOutTime,
                       TurnBackTime: $scope.StaffObj.TurnBackTime,
                       StartBreakTime: $routeParams.recordDate,
                       EndBreakTime: $scope.StaffObj.EndBreakTime
                   }

                $http.put(
                    service.locationUrl + "/CompanyStaffTimeTable/" + $routeParams.StaffTimeTableId,
                    objStaffTime)
                .success(function () {
                    $location.path($scope.path);
                })
                .error(function () { });
            }
            //End
            //update staff time table on End Break case
            if ($routeParams.checkType == 'End Break') {
                var objStaffTime =
                   {
                       CompanyStaffId: $scope.StaffObj.CompanyStaffId,
                       StampDate: $scope.StaffObj.StampDate,
                       StoreId: $routeParams.storeID,
                       UpdateTime: dateUpdate,
                       Remark: $routeParams.remark,
                       CheckInTime: $scope.StaffObj.CheckInTime,
                       CheckOutTime: $scope.StaffObj.CheckOutTime,
                       GoOutTime: $scope.StaffObj.GoOutTime,
                       TurnBackTime: $scope.StaffObj.TurnBackTime,
                       StartBreakTime: $scope.StaffObj.StartBreakTime,
                       EndBreakTime: $routeParams.recordDate
                   }

                $http.put(
                    service.locationUrl + "/CompanyStaffTimeTable/" + $routeParams.StaffTimeTableId,
                    objStaffTime)
                .success(function () {
                    $location.path($scope.path);
                })
                .error(function () { });
            }
            //End
            //update staff time table on Go Out case
            if ($routeParams.checkType == 'Go Out') {
                var objStaffTime =
                   {
                       CompanyStaffId: $scope.StaffObj.CompanyStaffId,
                       StampDate: $scope.StaffObj.StampDate,
                       StoreId: $routeParams.storeID,
                       UpdateTime: dateUpdate,
                       Remark: $routeParams.remark,
                       CheckInTime: $scope.StaffObj.CheckInTime,
                       CheckOutTime: $scope.StaffObj.CheckOutTime,
                       GoOutTime: $routeParams.recordDate,
                       TurnBackTime: $scope.StaffObj.TurnBackTime,
                       StartBreakTime: $scope.StaffObj.StartBreakTime,
                       EndBreakTime: $scope.StaffObj.EndBreakTime
                   }

                $http.put(
                    service.locationUrl + "/CompanyStaffTimeTable/" + $routeParams.StaffTimeTableId,
                    objStaffTime)
                .success(function () {
                    $location.path($scope.path);
                })
                .error(function () { });
            }
            //End
            //update staff time table on Turn Back case
            if ($routeParams.checkType == 'Turn Back') {
                var objStaffTime =
                   {
                       CompanyStaffId: $scope.StaffObj.CompanyStaffId,
                       StampDate: $scope.StaffObj.StampDate,
                       StoreId: $routeParams.storeID,
                       UpdateTime: dateUpdate,
                       Remark: $routeParams.remark,
                       CheckInTime: $scope.StaffObj.CheckInTime,
                       CheckOutTime: $scope.StaffObj.CheckOutTime,
                       GoOutTime: $scope.StaffObj.GoOutTime,
                       TurnBackTime: $routeParams.recordDate,
                       StartBreakTime: $scope.StaffObj.StartBreakTime,
                       EndBreakTime: $scope.StaffObj.EndBreakTime
                   }

                $http.put(
                    service.locationUrl + "/CompanyStaffTimeTable/" + $routeParams.StaffTimeTableId,
                    objStaffTime)
                .success(function () {
                    $location.path($scope.path);
                })
                .error(function () { });
            }
            //End
        }
    }
}

